package service

import (
	"context"

	"gitee.com/hust-ideas/little-genius-backend/dal/db/user"
	model "gitee.com/hust-ideas/little-genius-backend/model"
	"gitee.com/hust-ideas/little-genius-backend/util/pwd"
)

// AuthRegisterPost - 用户注册
func (s *ServiceContext) AuthRegisterPost(req model.AuthRegisterReq, c context.Context) (resp *model.AuthRegisterResp, err error) {
	// 检查用户 email 是否已经存在
	if _, err = s.Model.UserModel.FindOneByEmail(req.Email); err == nil {
		// err == nil 说明用户 email 已经存在
		return nil, model.ErrUserEmailAlreadyExists
	}
	password, error := pwd.HashPassword(req.Password)
	if error != nil {
		return nil, error
	}
	// 创建用户
	err = s.Model.UserModel.Insert(c, &user.User{
		UName:     req.Username,
		UEmail:    req.Email,
		UPassword: password,
	})
	if err != nil {
		return nil, err
	}
	// 3. 返回
	resp = &model.AuthRegisterResp{}
	return
}
